﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Collections;

namespace AlgorithmTester
{
    public class DAL
    {
        private string connectionString;
        private DbProviderFactory dbProviderFactory;

        public DAL()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            this.connectionString = config.AppSettings.Settings["DBConnectionString"].Value;
            this.dbProviderFactory = DbProviderFactories.GetFactory("System.Data.SqlClient ");
        }

        public List<int[]> ReadTrainingRatings(string tableName)
        {
            List<int[]> ratings = new List<int[]>();
            try
            {
                string sqlText = "select * from " + tableName;
                DbCommand command = dbProviderFactory.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = sqlText;

                using (DbConnection dbConnection = dbProviderFactory.CreateConnection())
                {
                    dbConnection.ConnectionString = this.connectionString;
                    dbConnection.Open();
                    command.Connection = dbConnection;

                    using (IDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            int[] record = new int[3];
                            record[0] = Convert.ToInt32(reader[0]);
                            record[1] = Convert.ToInt32(reader[1]);
                            record[2] = Convert.ToInt32(reader[2]);

                            ratings.Add(record);
                        }
                    }
                }

                return ratings;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
